feat(supervisor): wide events + warm-start trace propagation#3669
feat(supervisor): wide events + warm-start trace propagation#3669nicktrn wants to merge 6 commits into
Conversation
|
WalkthroughThis PR adds a supervisor-wide "wide events" observability system: new wideEvents modules provide types (State/Phase/Error), traceparent parsing, AsyncLocalStorage context, phase timing/recording, JSON serialization (emit) to stdout, lifecycle middleware (runWideEvent/emitOneShot) and helpers (setMeta/setExtra). Tests cover parsing, state creation, recording, emission, and middleware. Environment flags TRIGGER_WIDE_EVENTS_ENABLED and TRIGGER_WIDE_EVENTS_NOISY_ROUTES gate behavior. Supervisor, ComputeSnapshotService, WorkloadServer, and compute client/manager are wired to create and propagate wide events across dequeue, HTTP routes, socket lifecycle, and outbound compute calls. Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
671b137 to
3330bdc
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (2)
apps/supervisor/src/wideEvents/emit.test.ts (1)
97-102: ⚡ Quick winStrengthen truncation coverage with a UTF-8 multibyte case.
The current check validates character-count truncation only; adding a multibyte message case will prevent byte-limit regressions.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/supervisor/src/wideEvents/emit.test.ts` around lines 97 - 102, The existing test for truncation only checks character count; add a UTF-8 multibyte case to ensure truncation is done by byte length not characters: create a new test (similar to the "truncates very long error messages" one) that uses newState and captureEmit, sets s.error.message to a long repeated multibyte sequence (e.g., emoji or non-ASCII characters) whose character count is less than but byte length exceeds 512, then assert that (out["error.message"] as string).length in bytes (or Buffer.byteLength of the emitted string) is 512; reference the same helpers newState and captureEmit and the s.error structure to implement this check.apps/supervisor/src/wideEvents/record.test.ts (1)
34-40: ⚡ Quick winAdd a multibyte truncation test to match the byte-limit contract.
Current truncation coverage only uses ASCII; add a UTF-8 multibyte case so byte-capped behavior is protected.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/supervisor/src/wideEvents/record.test.ts` around lines 34 - 40, Add a new test that verifies recordPhase's truncation enforces a 512-byte limit for multibyte (UTF-8) characters: create a state with makeState(), call recordPhase(s, "x", performance.now(), new Error("<multibyte string>")) where the error message is a repeated multibyte character (e.g. "あ") long enough to exceed 512 bytes, then read phase = s.phases[0] and assert that Buffer.byteLength(phase.errorMsg!, "utf8") === 512 and that phase.errorMsg!.length is less than the original string length; reference the existing test pattern around makeState and recordPhase to add this case.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/supervisor/src/wideEvents/emit.ts`:
- Around line 36-40: The current truncation uses code units and can exceed
MAX_ERROR_MSG_BYTES for multibyte UTF‑8 characters; change the logic to truncate
by bytes: create a UTF‑8 buffer from state.error.message
(Buffer.from(state.error.message, 'utf8')), if buffer.length <=
MAX_ERROR_MSG_BYTES use the original message, otherwise slice the buffer to
MAX_ERROR_MSG_BYTES (buffer.slice(0, MAX_ERROR_MSG_BYTES)) and convert back to a
string via toString('utf8') so you get a byte-safe truncated msg before calling
appendIfSet("error.message", msg); update the code around state.error.message,
MAX_ERROR_MSG_BYTES, and msg in emit.ts accordingly.
In `@apps/supervisor/src/wideEvents/middleware.ts`:
- Line 72: Wrap each call to emit(state) in a non-throwing try/catch so
wide-event emission is best-effort: for the three occurrences of emit(state)
(the calls in this file), catch any error and log it via the module's logger (or
console.error if no logger is available) but do not rethrow or change the
returned result/flow; ensure the catch only handles emission failures and does
not swallow or override any existing error the surrounding operation may be
propagating.
In `@apps/supervisor/src/wideEvents/record.ts`:
- Around line 35-37: The code currently truncates p.errorMsg by JS string length
(msg.slice) which can exceed MAX_ERROR_MSG_BYTES when characters are multi-byte;
instead compute UTF-8 bytes via Buffer.from(msg, 'utf8'), if buf.length >
MAX_ERROR_MSG_BYTES slice the buffer to MAX_ERROR_MSG_BYTES and then trim any
trailing continuation bytes (drop bytes while (buf[last] >> 6) === 2) to avoid
cutting a multi-byte sequence, finally set p.errorMsg = buf.toString('utf8');
use the existing symbols msg, MAX_ERROR_MSG_BYTES, and p.errorMsg to locate and
replace the current truncation logic.
---
Nitpick comments:
In `@apps/supervisor/src/wideEvents/emit.test.ts`:
- Around line 97-102: The existing test for truncation only checks character
count; add a UTF-8 multibyte case to ensure truncation is done by byte length
not characters: create a new test (similar to the "truncates very long error
messages" one) that uses newState and captureEmit, sets s.error.message to a
long repeated multibyte sequence (e.g., emoji or non-ASCII characters) whose
character count is less than but byte length exceeds 512, then assert that
(out["error.message"] as string).length in bytes (or Buffer.byteLength of the
emitted string) is 512; reference the same helpers newState and captureEmit and
the s.error structure to implement this check.
In `@apps/supervisor/src/wideEvents/record.test.ts`:
- Around line 34-40: Add a new test that verifies recordPhase's truncation
enforces a 512-byte limit for multibyte (UTF-8) characters: create a state with
makeState(), call recordPhase(s, "x", performance.now(), new Error("<multibyte
string>")) where the error message is a repeated multibyte character (e.g. "あ")
long enough to exceed 512 bytes, then read phase = s.phases[0] and assert that
Buffer.byteLength(phase.errorMsg!, "utf8") === 512 and that
phase.errorMsg!.length is less than the original string length; reference the
existing test pattern around makeState and recordPhase to add this case.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: a20c1096-8c6a-468a-9621-0a1eb47e4bfd
📒 Files selected for processing (23)
.server-changes/README.md.server-changes/supervisor-compute-traceparent-forwarding.md.server-changes/supervisor-snapshot-lifecycle-events.md.server-changes/supervisor-wide-events.mdapps/supervisor/src/env.tsapps/supervisor/src/index.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/context.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/index.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/state.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/workloadServer/index.tsinternal-packages/compute/src/client.ts
✅ Files skipped from review due to trivial changes (6)
- .server-changes/supervisor-wide-events.md
- .server-changes/README.md
- .server-changes/supervisor-snapshot-lifecycle-events.md
- .server-changes/supervisor-compute-traceparent-forwarding.md
- apps/supervisor/src/wideEvents/context.ts
- apps/supervisor/src/wideEvents/state.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (38)
- GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (5, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (1, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (6, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (4, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (3, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (3, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 8)
- GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (8, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (2, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (4, 8)
- GitHub Check: internal / 🧪 Unit Tests: Internal (7, 8)
- GitHub Check: typecheck / typecheck
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
- GitHub Check: units / e2e-webapp / 🧪 E2E Tests: Webapp
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
- GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
- GitHub Check: typecheck / typecheck
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
- GitHub Check: Analyze (javascript-typescript)
🧰 Additional context used
📓 Path-based instructions (11)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{ts,tsx}: Use types over interfaces for TypeScript
Avoid using enums; prefer string unions or const objects insteadAlways import tasks from
@trigger.dev/sdk. Never use@trigger.dev/sdk/v3or deprecatedclient.defineJob.
Files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use function declarations instead of default exports
**/*.{ts,tsx,js,jsx}: Inpackages/core(@trigger.dev/core), import subpaths only, never import from root.
Add crumbs as you write code using//@Crumbscomments or `// `#region` `@crumbsblocks for debug tracing. They should be stripped byagentcrumbs stripbefore merge.
Files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
**/*.ts
📄 CodeRabbit inference engine (.cursor/rules/otel-metrics.mdc)
**/*.ts: When creating or editing OTEL metrics (counters, histograms, gauges), ensure metric attributes have low cardinality by using only enums, booleans, bounded error codes, or bounded shard IDs
Do not use high-cardinality attributes in OTEL metrics such as UUIDs/IDs (envId, userId, runId, projectId, organizationId), unbounded integers (itemCount, batchSize, retryCount), timestamps (createdAt, startTime), or free-form strings (errorMessage, taskName, queueName)
When exporting OTEL metrics via OTLP to Prometheus, be aware that the exporter automatically adds unit suffixes to metric names (e.g., 'my_duration_ms' becomes 'my_duration_ms_milliseconds', 'my_counter' becomes 'my_counter_total'). Account for these transformations when writing Grafana dashboards or Prometheus queries
Files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
**/*.{js,jsx,ts,tsx,json,md,yml,yaml}
📄 CodeRabbit inference engine (AGENTS.md)
Code formatting must be enforced using Prettier before committing
Files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
apps/supervisor/src/workloadManager/**/*.{js,ts}
📄 CodeRabbit inference engine (apps/supervisor/CLAUDE.md)
Container orchestration abstraction (Docker or Kubernetes) should be implemented in
src/workloadManager/
Files:
apps/supervisor/src/workloadManager/compute.ts
apps/supervisor/src/env.ts
📄 CodeRabbit inference engine (apps/supervisor/CLAUDE.md)
Environment configuration should be defined in
src/env.ts
Files:
apps/supervisor/src/env.ts
**/*.{test,spec}.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use vitest for all tests in the Trigger.dev repository
Files:
apps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/wideEvents/new.test.ts
**/*.test.ts
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.test.ts: Use vitest exclusively for testing. Never mock anything - use testcontainers instead.
Place test files next to source files with the naming conventionSourceFile.ts->SourceFile.test.ts
Files:
apps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/wideEvents/new.test.ts
**/*.test.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.test.{ts,tsx,js,jsx}: Test files should live beside the files under test and use descriptive describe and it blocks
Unit tests should use vitest framework
Tests should avoid mocks or stubs and use helpers from@internal/testcontainerswhen Redis or Postgres are needed
Files:
apps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/wideEvents/new.test.ts
apps/supervisor/src/services/**/*.{js,ts}
📄 CodeRabbit inference engine (apps/supervisor/CLAUDE.md)
Core service logic should be organized in the
src/services/directory
Files:
apps/supervisor/src/services/computeSnapshotService.ts
apps/supervisor/src/workloadServer/**/*.{js,ts}
📄 CodeRabbit inference engine (apps/supervisor/CLAUDE.md)
HTTP server for workload communication (heartbeats, snapshots) should be implemented in
src/workloadServer/
Files:
apps/supervisor/src/workloadServer/index.ts
🧠 Learnings (5)
📚 Learning: 2026-03-22T13:26:12.060Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3244
File: apps/webapp/app/components/code/TextEditor.tsx:81-86
Timestamp: 2026-03-22T13:26:12.060Z
Learning: In the triggerdotdev/trigger.dev codebase, do not flag `navigator.clipboard.writeText(...)` calls for `missing-await`/`unhandled-promise` issues. These clipboard writes are intentionally invoked without `await` and without `catch` handlers across the project; keep that behavior consistent when reviewing TypeScript/TSX files (e.g., usages like in `apps/webapp/app/components/code/TextEditor.tsx`).
Applied to files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
📚 Learning: 2026-03-22T19:24:14.403Z
Learnt from: matt-aitken
Repo: triggerdotdev/trigger.dev PR: 3187
File: apps/webapp/app/v3/services/alerts/deliverErrorGroupAlert.server.ts:200-204
Timestamp: 2026-03-22T19:24:14.403Z
Learning: In the triggerdotdev/trigger.dev codebase, webhook URLs are not expected to contain embedded credentials/secrets (e.g., fields like `ProjectAlertWebhookProperties` should only hold credential-free webhook endpoints). During code review, if you see logging or inclusion of raw webhook URLs in error messages, do not automatically treat it as a credential-leak/secrets-in-logs issue by default—first verify the URL does not contain embedded credentials (for example, no username/password in the URL, no obvious secret/token query params or fragments). If the URL is credential-free per this project’s conventions, allow the logging.
Applied to files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
📚 Learning: 2026-05-18T08:21:27.694Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3632
File: apps/webapp/sentry.server.ts:4-21
Timestamp: 2026-05-18T08:21:27.694Z
Learning: When handling Prisma error P1001 ("Can't reach database server") in TypeScript, don’t assume a single error shape. Prisma can surface P1001 via two different error classes/fields: `PrismaClientKnownRequestError` exposes it as `err.code === "P1001"` (common during mid-query connection drops), while `PrismaClientInitializationError` exposes it as `err.errorCode === "P1001"` (common on client startup failure). Therefore, predicates should use `err.code === "P1001" || err.errorCode === "P1001"`. Do not flag `err.code === "P1001"` as “unreachable/never matches,” as it is expected in production.
Applied to files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
📚 Learning: 2026-05-18T08:21:27.694Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3632
File: apps/webapp/sentry.server.ts:4-21
Timestamp: 2026-05-18T08:21:27.694Z
Learning: When handling Prisma errors for P1001 ("Can't reach database server"), do not assume it only appears under a single property name. Prisma may surface P1001 via either `PrismaClientKnownRequestError` (`err.code === "P1001"`, e.g., mid-query connection drops) or `PrismaClientInitializationError` (`err.errorCode === "P1001"`, e.g., client startup connection failure). To reliably detect the condition, check `err.code === "P1001" || err.errorCode === "P1001"`, and avoid review rules that would incorrectly flag `err.code === "P1001"` as unreachable/never-matching.
Applied to files:
apps/supervisor/src/wideEvents/index.tsinternal-packages/compute/src/client.tsapps/supervisor/src/workloadManager/compute.tsapps/supervisor/src/env.tsapps/supervisor/src/wideEvents/traceparent.tsapps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/new.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/services/computeSnapshotService.tsapps/supervisor/src/wideEvents/record.tsapps/supervisor/src/wideEvents/emit.tsapps/supervisor/src/wideEvents/middleware.tsapps/supervisor/src/workloadServer/index.tsapps/supervisor/src/wideEvents/new.test.tsapps/supervisor/src/index.ts
📚 Learning: 2026-05-18T14:40:02.173Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3658
File: packages/core/src/v3/realtimeStreams/manager.test.ts:1-147
Timestamp: 2026-05-18T14:40:02.173Z
Learning: In the triggerdotdev/trigger.dev repo, the policy “Never mock anything — use testcontainers instead” should only be enforced for integration tests that interact with real external services (e.g., Redis, Postgres) via actual infrastructure. For unit tests that exercise pure in-memory logic (e.g., cache semantics) it is OK to stub collaborators such as `ApiClient` using Vitest (`vi.fn()`) to assert call counts or control behavior. Do not flag `vi.fn()`-based `ApiClient` stubs in unit tests as violations of the testcontainers policy.
Applied to files:
apps/supervisor/src/wideEvents/middleware.test.tsapps/supervisor/src/wideEvents/emit.test.tsapps/supervisor/src/wideEvents/record.test.tsapps/supervisor/src/wideEvents/traceparent.test.tsapps/supervisor/src/wideEvents/new.test.ts
🔇 Additional comments (30)
apps/supervisor/src/env.ts (1)
260-267: LGTM!apps/supervisor/src/index.ts (6)
31-38: LGTM!Also applies to: 61-66, 257-276
286-304: LGTM!
306-386: LGTM!
388-452: LGTM!
475-476: LGTM!
493-547: LGTM!apps/supervisor/src/services/computeSnapshotService.ts (5)
9-17: LGTM!Also applies to: 36-36, 50-50, 56-56
75-108: LGTM!
110-131: LGTM!
148-205: LGTM!
244-269: LGTM!apps/supervisor/src/workloadServer/index.ts (9)
34-41: LGTM!Also applies to: 78-80, 88-89, 119-120, 127-127
161-209: LGTM!
225-258: LGTM!
260-332: LGTM!
334-427: LGTM!
429-503: LGTM!
505-589: LGTM!
661-679: LGTM!
681-707: LGTM!Also applies to: 716-726, 759-776
apps/supervisor/src/workloadManager/compute.ts (1)
13-13: LGTM!Also applies to: 46-64
internal-packages/compute/src/client.ts (1)
10-21: LGTM!Also applies to: 45-59
apps/supervisor/src/wideEvents/traceparent.ts (1)
11-39: LGTM!apps/supervisor/src/wideEvents/traceparent.test.ts (1)
1-44: LGTM!apps/supervisor/src/wideEvents/new.ts (1)
13-76: LGTM!apps/supervisor/src/wideEvents/new.test.ts (1)
1-82: LGTM!apps/supervisor/src/wideEvents/middleware.ts (1)
1-71: LGTM!Also applies to: 86-108, 110-123
apps/supervisor/src/wideEvents/middleware.test.ts (1)
1-208: LGTM!apps/supervisor/src/wideEvents/index.ts (1)
1-29: LGTM!
| const msg = | ||
| state.error.message.length > MAX_ERROR_MSG_BYTES | ||
| ? state.error.message.slice(0, MAX_ERROR_MSG_BYTES) | ||
| : state.error.message; | ||
| appendIfSet(out, "error.message", msg); |
There was a problem hiding this comment.
error.message truncation should be byte-safe.
Line 37 currently truncates by code units, so UTF-8 multibyte messages can exceed the intended 512-byte cap.
Proposed fix
- const msg =
- state.error.message.length > MAX_ERROR_MSG_BYTES
- ? state.error.message.slice(0, MAX_ERROR_MSG_BYTES)
- : state.error.message;
+ const msg = truncateUtf8(state.error.message, MAX_ERROR_MSG_BYTES);
appendIfSet(out, "error.message", msg);+function truncateUtf8(value: string, maxBytes: number): string {
+ if (Buffer.byteLength(value, "utf8") <= maxBytes) return value;
+ let out = value;
+ while (Buffer.byteLength(out, "utf8") > maxBytes) {
+ out = out.slice(0, -1);
+ }
+ return out;
+}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const msg = | |
| state.error.message.length > MAX_ERROR_MSG_BYTES | |
| ? state.error.message.slice(0, MAX_ERROR_MSG_BYTES) | |
| : state.error.message; | |
| appendIfSet(out, "error.message", msg); | |
| function truncateUtf8(value: string, maxBytes: number): string { | |
| if (Buffer.byteLength(value, "utf8") <= maxBytes) return value; | |
| let out = value; | |
| while (Buffer.byteLength(out, "utf8") > maxBytes) { | |
| out = out.slice(0, -1); | |
| } | |
| return out; | |
| } | |
| const msg = truncateUtf8(state.error.message, MAX_ERROR_MSG_BYTES); | |
| appendIfSet(out, "error.message", msg); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/supervisor/src/wideEvents/emit.ts` around lines 36 - 40, The current
truncation uses code units and can exceed MAX_ERROR_MSG_BYTES for multibyte
UTF‑8 characters; change the logic to truncate by bytes: create a UTF‑8 buffer
from state.error.message (Buffer.from(state.error.message, 'utf8')), if
buffer.length <= MAX_ERROR_MSG_BYTES use the original message, otherwise slice
the buffer to MAX_ERROR_MSG_BYTES (buffer.slice(0, MAX_ERROR_MSG_BYTES)) and
convert back to a string via toString('utf8') so you get a byte-safe truncated
msg before calling appendIfSet("error.message", msg); update the code around
state.error.message, MAX_ERROR_MSG_BYTES, and msg in emit.ts accordingly.
| } else { | ||
| state.ok = true; | ||
| } | ||
| emit(state); |
There was a problem hiding this comment.
Make wide-event emission best-effort (non-fatal).
emit(state) is currently allowed to throw on Line 72, Line 84, and Line 109. That can fail a successful operation (or mask the original thrown error), making observability impact business flow.
Suggested fix
import { emit } from "./emit.js";
import { newState, type Env } from "./new.js";
import { wideEventStorage } from "./context.js";
import type { State } from "./state.js";
+function emitSafely(state: State): void {
+ try {
+ emit(state);
+ } catch {
+ // best-effort observability: never break request flow
+ }
+}
+
/**
* Runs `fn` inside an AsyncLocalStorage state and emits one wide event on
@@
- emit(state);
+ emitSafely(state);
return result;
} catch (err) {
@@
- emit(state);
+ emitSafely(state);
throw err;
}
}
@@
- emit(state);
+ emitSafely(state);
}Also applies to: 84-85, 109-109
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/supervisor/src/wideEvents/middleware.ts` at line 72, Wrap each call to
emit(state) in a non-throwing try/catch so wide-event emission is best-effort:
for the three occurrences of emit(state) (the calls in this file), catch any
error and log it via the module's logger (or console.error if no logger is
available) but do not rethrow or change the returned result/flow; ensure the
catch only handles emission failures and does not swallow or override any
existing error the surrounding operation may be propagating.
| const msg = err.message; | ||
| p.errorMsg = msg.length > MAX_ERROR_MSG_BYTES ? msg.slice(0, MAX_ERROR_MSG_BYTES) : msg; | ||
| } |
There was a problem hiding this comment.
Enforce the 512-byte cap using UTF-8 bytes, not string length.
Line 36 truncates by code units, so multibyte messages can exceed MAX_ERROR_MSG_BYTES despite the byte-limit contract.
Proposed fix
- const msg = err.message;
- p.errorMsg = msg.length > MAX_ERROR_MSG_BYTES ? msg.slice(0, MAX_ERROR_MSG_BYTES) : msg;
+ const msg = err.message;
+ p.errorMsg = truncateUtf8(msg, MAX_ERROR_MSG_BYTES);
}+function truncateUtf8(value: string, maxBytes: number): string {
+ if (Buffer.byteLength(value, "utf8") <= maxBytes) return value;
+ let out = value;
+ while (Buffer.byteLength(out, "utf8") > maxBytes) {
+ out = out.slice(0, -1);
+ }
+ return out;
+}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/supervisor/src/wideEvents/record.ts` around lines 35 - 37, The code
currently truncates p.errorMsg by JS string length (msg.slice) which can exceed
MAX_ERROR_MSG_BYTES when characters are multi-byte; instead compute UTF-8 bytes
via Buffer.from(msg, 'utf8'), if buf.length > MAX_ERROR_MSG_BYTES slice the
buffer to MAX_ERROR_MSG_BYTES and then trim any trailing continuation bytes
(drop bytes while (buf[last] >> 6) === 2) to avoid cutting a multi-byte
sequence, finally set p.errorMsg = buf.toString('utf8'); use the existing
symbols msg, MAX_ERROR_MSG_BYTES, and p.errorMsg to locate and replace the
current truncation logic.
Adds wide-event observability for the supervisor: one flat-keyed JSON line per dequeue iteration, workload-server route, and run socket lifecycle event. Events carry
trace_idsourced from the inbound W3C traceparent plusmeta.run_idand related identifiers, so they join across services by run.The outbound warm-start POST also forwards the inbound traceparent so the upstream receiver continues the same trace instead of minting a new one.
Off by default behind
TRIGGER_WIDE_EVENTS_ENABLED. With the flag off, no events are emitted, no ALS state is allocated, and the outbound warm-start request is unchanged — every call site was audited to confirm the off path is byte-identical to current behavior.Dequeue-path phase timings recorded under
phase.<name>.duration_ms:restore,warm_start,workload_create. Apath_takenextra distinguishesrestore/warm_start/cold_create/skipped_no_image.Refs TRI-9480.